#region <!--##session BOEditWholeClass##-->
	
		/*<!--##
		Entity entity = RepositoryManager.Instance.ActiveEntity;
		<%@EntityIncludes%>
		codeGeneratorVariable_DisplayCode.Append(GeneralUsing() + Environment.NewLine);
		string[] editKeyFields = entity.EditKey.Split(',');
		##-->*/
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.PresentationLayer;        
namespace <!--##=RepositoryManager.Instance.Repository.ProjectName##-->.Aspx.<!--##=accessorName##-->
{
	public partial class <!--##=className##-->Edit : EditParentPage,IEditPage
	{
		
        protected override string PageKey
        {
            get
            {
                if(IsUpdate)
                {
                    return "<!--##=className##-->Edit"; 
                }
                else
                {
                    return "<!--##=className##-->Add"; 
                }
            }
        }
        protected override void OnPreLoad(EventArgs e)
		{
            
			if (!IsPostBack)
			{
				if (IsUpdate)
				{
					
					if(
					/*<!--##
						string or="";			  
						foreach(string keyField in editKeyFields)
						{
					##-->*/
							<!--##=or##-->string.IsNullOrEmpty(Request.QueryString[<!--##=className##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(keyField)##-->])
					/*<!--##
							or = " || "; 
						}
					##-->*/
					)
					{
						throw Logger.Write(new Exception("Edit page requires 'key' for updating."));
					}

					// initialize key
					Pair[] key = new Pair[<!--##=editKeyFields.Length##-->];
					/*<!--##
						for(int i=0; i<editKeyFields.Length; i++)
						{
					##-->*/
					key[<!--##=i##-->] = new Pair(<!--##=className##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(editKeyFields[i])##-->, Request.QueryString[<!--##=className##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(editKeyFields[i])##-->]);
					/*<!--##
						}
					##-->*/
					Key = key;
                
				}
			}
			base.OnPreLoad(e);

		}

		protected override void OnLoad(EventArgs e)
		{


			if (!IsPostBack)
			{
				PopulateListControls();
				RowToControl(BOPaper as <!--##=className##-->Paper); 
			}

			base.OnLoad(e);
		}
	
		   
		/*<!--##
		 codeGeneratorVariable_DisplayCode.Append(BOEditPopulateListControls() + Environment.NewLine);
		 codeGeneratorVariable_DisplayCode.Append(BOEditControlToRow() + Environment.NewLine);
		 codeGeneratorVariable_DisplayCode.Append(BOEditRowToControl() + Environment.NewLine);
		##-->*/
		
	/*<!--##
	if(isUserRoleClass)
	{
	##-->*/
	protected void tvSecurityModules_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
	{
		SiteMapNode siteMapNode = e.Node.DataItem as SiteMapNode;
		e.Node.Value = siteMapNode.Key;
		if (IsUpdate)
		{
			e.Node.Checked = ((IRole)BO).IsUserAuthorizedForModule(e.Node.Value);
		}
	}	
	/*<!--##	
	} 
	##-->*/ 
	#region IEditPage Members

	public string EntityName
	{
		get { return "<!--##=entityName##-->"; }
	}

	public string ResourceName
	{
		get
		{

			return "<!--##=className##-->";
		}
	}
	BusinessObjectCollection IPage.BOCollection
	{
		get
		{
			return <!--##=collectionName##-->.Instance;
		}
	}

	BusinessObjectPaper IEditPage.GetBOPaper()
	{
		return new <!--##=className##-->Paper();
	}
	public MultiView MVMessage
	{
		get
		{
			return mvMessage;
		}
	}

	#endregion
	
		


		//**Event Handlers
		protected void btnSave_Click(object sender, EventArgs e)
		{
			<!--##=className##-->Paper row = BOPaper as <!--##=className##-->Paper;
			ControlToRow(row);

			<!--##=className##--> bo = new <!--##=className##-->(row);
	        try
			{
			    if (IsUpdate)
			    {
				    bo.Update(true);
			    }
			    else
			    {
				    bo.Insert(true);
			    }
	
			/*<!--##
			if(isUserLoginClass)
			{
			##-->*/
			
				bo.Children_<!--##=users_rolesClass##-->.DeleteRecords(true);
				foreach(string str in ((string)chkListSecurityGroups.Value).Split(','))
				{
					<!--##=users_rolesClass##--> newUsersRoles= bo.CreateNewChild_<!--##=users_rolesClass##-->();
					newUsersRoles[<!--##=users_rolesClass##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(userLogin.roleIdField)##-->] = str;
					newUsersRoles.Save(true);
				}
			
			/*<!--##	
			}
			else if(isUserRoleClass)
			{
			##-->*/
			
				bo.Children_<!--##=roles_modulesClass##-->.DeleteRecords(true);
				foreach(TreeNode node in tvSecurityModules.CheckedNodes)
				{
					<!--##=roles_modulesClass##--> newRolesModules= bo.CreateNewChild_<!--##=roles_modulesClass##-->();
					newRolesModules[<!--##=roles_modulesClass##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(userRole.moduleIdField)##-->] = node.Value;
					newRolesModules.Save(true);
				}
			 	
			/*<!--##	
			}
			##-->*/
			    DataAccessor.CommitTransaction();
                DoOnSuccessfullSave();
            }
			catch(Exception ex)
			{
				DataAccessor.RollbackTransaction();
				throw ex;
			}
			
		}

		private void DoOnSuccessfullSave()
		{
			<!--##=className##-->Paper row = BOPaper as <!--##=className##-->Paper;
			string queryString = string.Empty; 
			/*<!--##
				string seperator = " + \"&\" + ";
				if(gridKey!=string.Empty)
				{
			##-->*/
					queryString =  
			/*<!--##			  
					string[] strs=gridKey.Split(',');
					for(int i=0;  i<strs.Length; i++)
					{
						string keyField = strs[i];
						if(i == strs.Length-1)
						{
							seperator = ";";
						}
			##-->*/		
						string.Format("{0}={1}",
							<!--##=className##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(keyField)##-->,
							row.<!--##=StringProcessor.Property(keyField)##-->) <!--##=seperator##-->
			/*<!--##
						 
					}
					
				}	
		    ##-->*/

			Information=ResourceProvider.GetGeneralResourceString(
                (IsUpdate)? "Message_RecordUpdatedSuccessfully":
                    "Message_RecordAddedSuccessfully");


			Response.Redirect(string.Format("<!--##=className##-->Edit.aspx?Update=true&{0}", queryString), true);

		}

	}
}
	
<!--##/session##-->
#endregion

#region <!--##session BOEditPopulateListControls##-->
        /*<!--##
		Entity entity = RepositoryManager.Instance.ActiveEntity;
		<%@EntityIncludes%>
				
		//overriding entity.
		Entity entityBOPage =  RepositoryManager.Instance.Entities["BOPage",entityName];
		Entity entityBOEditPage =  RepositoryManager.Instance.Entities["BOEditPage",entityName];
		if(entityBOPage != null)
		{
			entityBOPage.OverridingEntity = entityBOEditPage;
		}
		entity.OverridingEntity = entityBOPage;

		##-->*/
        private void PopulateListControls()
        {
			IListControl ctrl = null;
        /*<!--##
        foreach(string entityFieldKey in entity.Fields.Keys)
        {
            Entity.Field entityField = entity.Fields[entityFieldKey];
            <%@EntityFieldIncludes%> 
            if(listTable != string.Empty)
            {
			codeGeneratorVariable_DisplayCode.Append(Environment.NewLine + Environment.NewLine);
        ##-->*/
            //**<!--##=entityFieldName##-->
			ctrl = <!--##=FreeHash.ControlPrefix[listControl]##--><!--##=property##-->;
			<!--##=listTableCollectionName##--> col<!--##=property##--> = <!--##=listTableCollectionName##-->.Instance;
			ctrl.Enumerable=col<!--##=property##-->;
			ctrl.FormatString = "<!--##=formatString##-->";
			ctrl.KeyField = <!--##=listTableClassName##-->.FieldNames.<!--##=listTableKeyFieldConstant##-->;
			ctrl.DisplayFieldList = new string[] {
			/*<!--##
				string qoma="";			  
                foreach(string displayField in listTableDisplayFields.Split(','))
                {
			##-->*/
					<!--##=qoma##--><!--##=listTableClassName##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(displayField)##-->
			/*<!--##
					qoma = ","; 
                }
		    ##-->*/
			};
			ctrl.DataBind();
           
            
        /*<!--##
            }  
        }
		if(isUserLoginClass)
		{
		##-->*/
			ctrl = chkListSecurityGroups;
			<!--##=roleCollection##--> colSecurityGroups = <!--##=roleCollection##-->.Instance;
			ctrl.Enumerable=colSecurityGroups;
			ctrl.FormatString = "{0}";
			
			ctrl.KeyField = <!--##=roleClass##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(userRole.roleIdField)##-->;
			ctrl.DisplayFieldList = new string[] {<!--##=roleClass##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(userRole.roleNameField)##-->
			};
			ctrl.DataBind();
		/*<!--##
		}
		##-->*/
		}
<!--##/session##-->
#endregion

#region <!--##session BOEditRowToControl##-->
        /*<!--##
		Entity entity = RepositoryManager.Instance.ActiveEntity;
		<%@EntityIncludes%>
								
		//overriding entity.
		Entity entityBOPage =  RepositoryManager.Instance.Entities["BOPage",entityName];
		Entity entityBOEditPage =  RepositoryManager.Instance.Entities["BOEditPage",entityName];
		if(entityBOPage != null)
		{
			entityBOPage.OverridingEntity = entityBOEditPage;
		}
		entity.OverridingEntity = entityBOPage;

		##-->*/
		protected void RowToControl(<!--##=className##-->Paper row)
		{
            /*<!--##
            foreach(string entityFieldKey in entity.Fields.Keys)
            {
                Entity.Field entityField = entity.Fields[entityFieldKey];
                <%@EntityFieldIncludes%> 
				if(!displayed)
				{
					continue;
				}
				if(dataType == "shortbinary" || dataType == "binary")
				{
					//will handle it later
					continue;
				}
				codeGeneratorVariable_DisplayCode.Append(Environment.NewLine + Environment.NewLine);
				string usedControl = (listTable == string.Empty)? webControl: listControl;
            ##-->*/

			//**<!--##=entityFieldName##-->
			if (<!--##=FreeHash.ControlPrefix[usedControl]##--><!--##=property##--> != null)
			{
				<!--##=FreeHash.ControlPrefix[usedControl]##--><!--##=property##-->.Value = <!--##=strDecimalCast##--> row.<!--##=property##-->;
			}
		
			/*<!--##	
            }
			if(isUserLoginClass)
			{
			##-->*/
			if (IsUpdate)
			{
				chkListSecurityGroups.Value = ((<!--##=className##-->)BO).Children_<!--##=users_rolesClass##-->.GetFieldValues(<!--##=users_rolesClass##-->.FieldNames.<!--##=StringProcessor.ConstantNStatic(userLogin.roleIdField)##-->); 	
				
			}
			/*<!--##	
			}
			else if(isUserRoleClass)
			{
			##-->*/
			if (IsUpdate)
			{
				foreach (TreeNode node in tvSecurityModules.Nodes)
				{
					node.Checked= ((IRole)BO).IsUserAuthorizedForModule(node.Value);
				} 	
			}	
			/*<!--##	
			}
		    ##-->*/
		}
<!--##/session##-->
#endregion

#region <!--##session BOEditControlToRow##-->
        /*<!--##
		Entity entity = RepositoryManager.Instance.ActiveEntity;
		<%@EntityIncludes%>
								
		//overriding entity.
		Entity entityBOPage =  RepositoryManager.Instance.Entities["BOPage",entityName];
		Entity entityBOEditPage =  RepositoryManager.Instance.Entities["BOEditPage",entityName];
		if(entityBOPage != null)
		{
			entityBOPage.OverridingEntity = entityBOEditPage;
		}
		entity.OverridingEntity = entityBOPage;

		##-->*/
		protected void ControlToRow(<!--##=className##-->Paper row)
		{
            /*<!--##
            foreach(string entityFieldKey in entity.Fields.Keys)
            {
                Entity.Field entityField = entity.Fields[entityFieldKey];
                <%@EntityFieldIncludes%> 
				if(!displayed)
				{
					continue;
				}
				if(dataType == "shortbinary" || dataType == "binary")
				{
					//will handle it later
					continue;
				}
				
				if(!(isNative && !tokenEnabled && !autoIncreament))
				{
					continue;
				}
				codeGeneratorVariable_DisplayCode.Append(Environment.NewLine + Environment.NewLine);
				string usedControl = (listTable == string.Empty)? webControl: listControl;
				string strNullable = (isValueType)? "?": string.Empty; 

            ##-->*/

			//**<!--##=entityFieldName##-->
			/** Custom Region Start [ControlToRow(<!--##=entityFieldName##-->)] **/
			row.<!--##=property##--> = (<!--##=FreeHash.ControlPrefix[usedControl]##--><!--##=property##-->.Value != null) ? 
				Convert.To<!--##=cSharpFrameworkDataType##-->(<!--##=FreeHash.ControlPrefix[usedControl]##--><!--##=property##-->.Value) :
				(<!--##=cSharpKeywordDataType##--><!--##=strNullable##-->)null;
			/** Custom Region End **/
			/*<!--##	
            }
		    ##-->*/
		}
<!--##/session##-->
#endregion



